<?php
session_start();
include("load-settings.php");

if(!isset($_SESSION['user']))
	header("Location: login.php");
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>Veteran Boosters Order Tracking</title>
		<meta charset="UTF-8" />
        <link rel="icon" href="http://www.veteranboosters.com/wp-content/uploads/2014/04/favicon4.png" type="image/x-icon"/>
        <link rel="shortcut icon" href="http://www.veteranboosters.com/wp-content/uploads/2014/04/favicon4.png" type="image/x-icon"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<link rel="stylesheet" href="css/bootstrap.min.css" />
		<link rel="stylesheet" href="css/font-awesome.css" />
		<link rel="stylesheet" href="css/fullcalendar.css" />
		<link rel="stylesheet" href="css/jquery.jscrollpane.css" />	
		<link rel="stylesheet" href="css/unicorn.css" />
		<!--[if lt IE 9]>
		<script type="text/javascript" src="js/respond.min.js"></script>
		<![endif]-->

		<link href="stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />
		<script src="javascripts/application.js" type="text/javascript"></script>	
	</head>	
	

<?php
	include("top.php");
?>

  <div class="container-fluid">
					
					<div class="row-fluid">
						<div class="col-xs-12">
							<div class="alert alert-info">
								Welcome in the <strong>Veteran Boosters Order Tracking</strong>. Don't forget to check all the pages!
								<a href="#" data-dismiss="alert" class="close">×</a>
							</div>
							<div class="row-fluid">
									<div class="alert alert-info alert-block">
										   <?php
												echo stripslashes($home_message);
										   ?>
										</div>
										<div class = "alert alert-info">
										<?php
										if($type == 0)
												echo stripslashes($customer_message); 
											else if($type == 1)
												echo stripslashes($booster_message);
											else if($type == 2)
												echo stripslashes($admin_message);
												?>
												</div>
							</div>
							<div class="widget-box">
								<div class="widget-title">
									<span class="icon"><i class="fa fa-signal"></i></span>
									<h5>Open tickets</h5>
									
								</div>
								<div class="widget-content">
									<div class="row">
										<div class="col-xs-12 col-lg-12 col-md-12 col-sm-12">
											<table class="table table-bordered table-striped table-hover">
										   <thead>
											  <tr>
												 <th>User</th>
												 <th>Order</th>
												 <th>Status</th>
												 <th>Actions</th>
											  </tr>
										   </thead>
										   <tbody>
										   <?php
												if($type != 2 && $type != 3)
												{
													$result = mysql_query("SELECT * FROM ticket WHERE user = $user AND status = 0");

													while($row = mysql_fetch_array($result))
													{
														echo "<tr><td>".$row['user']."</td>";
														echo "<td>".$row['order_record'].$row['duo_order']."</td>";
														echo "<td>Pending</td>";
														echo "<td><a href='ticket.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td></tr>";
													}
												}
												else
												{
													$result = mysql_query("SELECT * FROM ticket WHERE status = 0");

													while($row = mysql_fetch_array($result))
													{
														echo "<tr><td>".$row['user']."</td>";
														echo "<td>".$row['order_record']."</td>";
														echo "<td>Pending</td>";
														echo "<td><a href='ticket.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td></tr>";
													}
												}
										   ?>
										   </tbody>
										</table>
										</div>
										
									</div>							
								</div>
							</div>					
						</div>
					</div>
					<div class="row-fluid">
						<div class="col-xs-12 col-sm-12 col-md-12">
							<div class="widget-box">
								<div class="widget-title">
								<span class="icon"><i class="fa fa-file"></i></span><h5>Boosting Orders</h5>
								</div>
								<div class="widget-content nopadding">
									<table class="table table-bordered table-striped table-hover">
               <thead>
                  <tr>
					 <th>Order ID</th>
					 <th>Booster</th>
					 <th>Summoner</th>
					 <th>Region</th>
                     <th>Details</th>
                     <th>Progress</th>
                     <th>Status</th>
                     <th>Actions</th>
                  </tr>
               </thead>
               <tbody>
			   <?php
					if($type == 0)
					{
						$result = mysql_query("SELECT * FROM order_record WHERE user = ".$_SESSION['user']." ORDER BY verified_complete ASC");

						if(mysql_num_rows($result) == 0)
						{
							echo "<tr><td colspan='4'>You don't have any orders.</td></tr>";
						}
						else
						{
							while($row = mysql_fetch_array($result))
							{
								echo "<tr><td>".$row['id']."</td>";

								$subresult = mysql_query("SELECT * FROM booster_order WHERE active <> 0 AND order_record = ".$row['id']);

								if(mysql_num_rows($subresult) == 0)
								{
									$cur_booster = "None";
								}
								else
								{
									$subrow = mysql_fetch_array($subresult);
									$boost_result = mysql_query("SELECT * FROM user WHERE id = ".$subrow['booster']);
									$boost_row = mysql_fetch_array($boost_result);
									$cur_booster = $boost_row['username'];
								}

								echo "<td>".$cur_booster."</td>";
								echo "<td>".$row['summoner']."</td>";
								echo "<td>".strtoupper($row['region'])."</td>";

								if($row['end_tier'] != null && $row['end_tier'] != "")
								{
									echo "<td>".$row['start_tier']." ".$row['start_division']." - ".$row['end_tier']." ".$row['end_division']."</td>";

									$subresult = mysql_query("SELECT * FROM booster_order WHERE active <> 0 AND order_record = ".$row['id']);

									if(mysql_num_rows($subresult) == 0)
									{
										echo "<td>Order pending</td>";
										echo "<td>Pending</td>";
										echo "<td><a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a> <a href='flag.php?id=".$row['id']."' class='btn btn-success btn-xs'>Flag for admin</a></td>";
									}
									else
									{
										$subrow = mysql_fetch_array($subresult);
										if($row['complete'] == 0)
											echo "<td>In progress</td>";
										else
											echo "<td>Complete</td>";

											
										if($subrow['active'] == 1)
										{
											echo "<td>Active</td>";
											//echo "<td><a href='#' class='btn btn-success btn-xs'>Pause</a>";
											echo "<td><a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a> <a href='flag.php?id=".$row['id']."' class='btn btn-success btn-xs'>Flag for admin</a></td>";
										}
										else if($subrow['active'] == 2)
										{
											echo "<td>Paused</td>";
											//echo "<td><a href='#' class='btn btn-success btn-xs'>Resume</a>";
											echo "<a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
										}
										else if($subrow['active'] == 3)
										{
											echo "<td>Admin reviewing</td>";
											echo "<td><a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
										}
										else if($subrow['active'] == 0 && $row['verified_complete'] == 1)
										{
											echo "<td>Complete</td>";
											echo "<td><a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a> <a href='flag.php?id=".$row['id']."' class='btn btn-success btn-xs'>Flag for admin</a></td>";
										}
										else if($subrow['active'] == 0 && $row['verified_complete'] == 0)
										{
											echo "<td>Admin verifying order completion</td>";
											echo "<td><a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a> <a href='flag.php?id=".$row['id']."' class='btn btn-success btn-xs'>Flag for admin</a></td>";
										}
									}

									echo "</tr>";
								}
								else
								{
									echo "<td>".$row['number_of_wins']." wins at ".$row['start_tier']." ".$row['start_division']."</td>";

									$subresult = mysql_query("SELECT * FROM booster_order WHERE active <> 0 AND order_record = ".$row['id']." ORDER BY id DESC");

									if(mysql_num_rows($subresult) == 0)
									{
										echo "<td>Order pending</td>";
										echo "<td>Pending</td>";
										echo "<td><a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a> <a href='flag.php?id=".$row['id']."' class='btn btn-success btn-xs'>Flag for admin</a></td>";
									}
									else
									{
										$subrow = mysql_fetch_array($subresult);
										$total_result = mysql_query("SELECT SUM(wins) AS all_wins FROM booster_order WHERE order_record = ".$row['id']);
										$total_row = mysql_fetch_array($total_result);
										$total_wins = $total_row['all_wins'];
										$total_result = mysql_query("SELECT SUM(losses) AS all_wins FROM booster_order WHERE order_record = ".$row['id']);
										$total_row = mysql_fetch_array($total_result);
										$total_losses = $total_row['all_wins'];
										echo "<td>".($total_wins-$total_losses)." of ".$row['number_of_wins']." wins</td>";

										if($subrow['active'] == 1)
										{
											echo "<td>Active</td>";
											//echo "<td><a href='#' class='btn btn-success btn-xs'>Pause</a>";
											echo "<td><a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a> <a href='flag.php?id=".$row['id']."' class='btn btn-success btn-xs'>Flag for admin</a></td>";
										}
										else if($subrow['active'] == 2)
										{
											echo "<td>Paused</td>";
											echo "<td><a href='#' class='btn btn-success btn-xs'>Resume</a>";
											echo "<a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
										}
										else if($subrow['active'] == 3)
										{
											echo "<td>Admin reviewing</td>";
											echo "<td><a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
										}
										else if($subrow['active'] == 0 && $row['verified_complete'] == 1)
										{
											echo "<td>Complete</td>";
											echo "<td><a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a> <a href='flag.php?id=".$row['id']."' class='btn btn-success btn-xs'>Flag for admin</a></td>";
										}
										else if($subrow['active'] == 0 && $row['verified_complete'] == 0)
										{
											echo "<td>Admin verifying order completion</td>";
											echo "<td><a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a> <a href='flag.php?id=".$row['id']."' class='btn btn-success btn-xs'>Flag for admin</a></td>";
										}
									}

									
									echo "</tr>";
								}
							}
						}
					}
					else if($type == 1)
					{
						$result = mysql_query("SELECT * FROM booster_order WHERE active <> 0 AND booster = ".$_SESSION['user']);

						if(mysql_num_rows($result) == 0)
						{
							echo "<tr><td colspan='4'>You don't have any active orders.</td></tr>";
						}
						else
						{
							while($row = mysql_fetch_array($result))
							{
								$order = $row['order_record'];

								$subresult = mysql_query("SELECT * FROM order_record WHERE id = $order");
								$subrow = mysql_fetch_array($subresult);

								echo "<tr><td>".$subrow['id']."</td>";

								$boost_result = mysql_query("SELECT * FROM user WHERE id = ".$_SESSION['user']);
								$boost_row = mysql_fetch_array($boost_result);
								$cur_booster = $boost_row['username'];

								echo "<td>".$cur_booster."</td>";
								echo "<td>".$row['summoner']."</td>";
								echo "<td>".strtoupper($row['region'])."</td>";

								if($subrow['end_tier'] != null && $subrow['end_tier'] != "")
								{
									echo "<td>".$subrow['start_tier']." ".$subrow['start_division']." - ".$subrow['end_tier']." ".$subrow['end_division']."</td>";

									echo "<td>".$row['wins']." wins</td>";

									if($row['active'] == 1)
									{
										echo "<td>Active</td>";
										echo "<td><a href='order.php?id=".$subrow['id']."' class='btn btn-success btn-xs'>Details</a></td>";
									}
									else if($row['active'] == 2)
									{
										echo "<td>Paused</td>";
										echo "<td><a href='order.php?id=".$subrow['id']."' class='btn btn-success btn-xs'>Details</a></td>";
									}
									else if($row['active'] == 3)
									{
										echo "<td>Admin reviewing</td>";
										echo "<td><a href='order.php?id=".$subrow['id']."' class='btn btn-success btn-xs'>Details</a></td>";
									}
									echo "</tr>";
								}
								else
								{
									echo "<td>".$subrow['number_of_wins']." wins at ".$subrow['start_tier']." ".$subrow['start_division']."</td>";

									echo "<td>".($row['wins']-$row['losses'])." of ".$subrow['number_of_wins']." wins</td>";

									if($row['active'] == 1)
									{
										echo "<td>Active</td>";
										echo "<td><a href='order.php?id=".$subrow['id']."' class='btn btn-success btn-xs'>Details</a><a href='unclaim.php?id=".$row['id']."' class='btn btn-success btn-xs'>Unclaim</a></td>";
									}
									else if($row['active'] == 2)
									{
										echo "<td>Paused</td>";
										echo "<td><a href='order.php?id=".$subrow['id']."' class='btn btn-success btn-xs'>Details</a><a href='unclaim.php?id=".$row['id']."' class='btn btn-success btn-xs'>Unclaim</a></td>";
									}
									else if($row['active'] == 3)
									{
										echo "<td>Admin reviewing</td>";
										echo "<td><a href='order.php?id=".$subrow['id']."' class='btn btn-success btn-xs'>Details</a><a href='unclaim.php?id=".$row['id']."' class='btn btn-success btn-xs'>Unclaim</a></td>";
									}
									echo "</tr>";
								}
							}
						}
					}
					else if($type == 2 || $type == 3)
					{
						$result = mysql_query("SELECT * FROM order_record WHERE complete = 0 ORDER BY timestamp DESC");

						if(mysql_num_rows($result) == 0)
						{
							echo "<tr><td colspan='4'>No orders.</td></tr>";
						}
						else
						{
							while($row = mysql_fetch_array($result))
							{
								echo "<tr><td>".$row['id']."</td>";

								$subresult = mysql_query("SELECT * FROM booster_order WHERE active <> 0 AND order_record = ".$row['id']) or die(mysql_error());

								if(mysql_num_rows($subresult) == 0)
								{
									$cur_booster = "None";
								}
								else
								{
									$subrow = mysql_fetch_array($subresult);
									$boost_result = mysql_query("SELECT * FROM user WHERE id = ".$subrow['booster']) or die(mysql_error());
									$boost_row = mysql_fetch_array($boost_result);
									$cur_booster = $boost_row['username'];
								}

								echo "<td>".$cur_booster."</td>";
								echo "<td>".$row['summoner']."</td>";
								echo "<td>".strtoupper($row['region'])."</td>";

								if($row['end_tier'] != null && $row['end_tier'] != "")
								{
									echo "<td>".$row['start_tier']." ".$row['start_division']." - ".$row['end_tier']." ".$row['end_division']."</td>";

									$subresult = mysql_query("SELECT * FROM booster_order WHERE active <> 0 AND order_record = ".$row['id']);

									if(mysql_num_rows($subresult) == 0)
									{
										echo "<td>Order pending</td>";
										echo "<td>Pending</td>";
										echo "<td><a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
									}
									else
									{
										$subrow = mysql_fetch_array($subresult);
										echo "<td>".$subrow['wins']." wins</td>";

										if($subrow['active'] == 1)
										{
											echo "<td>Active</td>";
											echo "<td>";
											echo "<a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
										}
										else if($subrow['active'] == 2)
										{
											echo "<td>Paused</td>";
											echo "<td><a href='#' class='btn btn-success btn-xs'>Resume</a>";
											echo "<a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
										}
										else if($subrow['active'] == 3)
										{
											echo "<td>Admin reviewing</td>";
											echo "<td><a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
										}
										else if($subrow['active'] == 0)
										{
											echo "<td>Complete</td>";
											echo "<td><a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
										}
									}

									
									echo "</tr>";
								}
								else
								{
									echo "<td>".$row['number_of_wins']." wins at ".$row['start_tier']." ".$row['start_division']."</td>";

									$subresult = mysql_query("SELECT * FROM booster_order WHERE active <> 0 AND order_record = ".$row['id']);

									if(mysql_num_rows($subresult) == 0)
									{
										echo "<td>Order pending</td>";
										echo "<td>Pending</td>";
										echo "<td><a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
									}
									else
									{
										$subrow = mysql_fetch_array($subresult);
										echo "<td>".($subrow['wins']-$subrow['losses'])." of ".$row['number_of_wins']." wins</td>";

										if($subrow['active'] == 1)
										{
											echo "<td>Active</td>";
											echo "<td>";
											echo "<a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
										}
										else if($subrow['active'] == 2)
										{
											echo "<td>Paused</td>";
											echo "<td>";
											echo "<a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
										}
										else if($subrow['active'] == 3)
										{
											echo "<td>Admin reviewing</td>";
											echo "<td><a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
										}
										else if($subrow['active'] == 0)
										{
											echo "<td>Complete</td>";
											echo "<td><a href='order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
										}
									}

									
									echo "</tr>";
								}
							}
						}
					}
				?>
			   </tbody>
            </table>
								</div>
							</div>
						</div>
						<div class="col-xs-12 col-sm-12 col-md-12">
							<div class="widget-box">
								<div class="widget-title">
								<span class="icon"><i class="fa fa-comment"></i></span><h5>Duo Queue Orders</h5>
								</div>
								<div class="widget-content nopadding">
									<table class="table table-bordered table-striped table-hover">
               <thead>
                  <tr>
					 <th>Order ID</th>
					 <th>Booster</th>
					 <th>Summoner</th>
					 <th>Region</th>
                     <th>Details</th>
                     <th>Progress</th>
                     <th>Status</th>
                     <th>Actions</th>
                  </tr>
               </thead>
               <tbody>
			   <?php
					if($type == 0)
					{
						$result = mysql_query("SELECT * FROM duo_order WHERE user = ".$_SESSION['user']." ORDER BY verified_complete ASC");

						if(mysql_num_rows($result) == 0)
						{
							echo "<tr><td colspan='4'>You don't have any orders.</td></tr>";
						}
						else
						{
							while($row = mysql_fetch_array($result))
							{
								echo "<tr><td>".$row['id']."</td>";

								$subresult = mysql_query("SELECT * FROM booster_duo WHERE active <> 0 AND duo_order = ".$row['id']) or die(mysql_error());

								if(mysql_num_rows($subresult) == 0)
								{
									$cur_booster = "None";
								}
								else
								{
									$subrow = mysql_fetch_array($subresult);
									$boost_result = mysql_query("SELECT * FROM user WHERE id = ".$subrow['booster']) or die(mysql_error());
									$boost_row = mysql_fetch_array($boost_result);
									$cur_booster = $boost_row['username'];
								}

								echo "<td>".$cur_booster."</td>";
								echo "<td>".$row['summoner']."</td>";
								echo "<td>".strtoupper($row['region'])."</td>";

								if($row['number_of_games'] > 0)
									echo "<td>".$row['number_of_games']." games at ".$row['start_tier']." ".$row['start_division']."</td>";
								else if($row['number_of_wins'] > 0)
									echo "<td>".$row['number_of_wins']." wins at ".$row['start_tier']." ".$row['start_division']."</td>";
								else if($row['number_of_coaches'] > 0)
									echo "<td>".$row['number_of_coaches']." coached games at ".$row['start_tier']." ".$row['start_division']."</td>";

								$subresult = mysql_query("SELECT * FROM booster_duo WHERE active <> 0 AND duo_order = ".$row['id']." ORDER BY id DESC");

								if(mysql_num_rows($subresult) == 0)
								{
									echo "<td>Order pending</td>";
									echo "<td>Pending</td>";
									echo "<td><a href='duo_order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a> <a href='flag_duo.php?id=".$row['id']."' class='btn btn-success btn-xs'>Flag for admin</a></td>";
								}
								else
								{
									$subrow = mysql_fetch_array($subresult);
									$total_result = mysql_query("SELECT SUM(wins) AS all_wins FROM booster_duo WHERE duo_order = ".$row['id']);
									$total_row = mysql_fetch_array($total_result);
									$total_wins = $total_row['all_wins'];
									$total_result = mysql_query("SELECT SUM(losses) AS all_wins FROM booster_duo WHERE duo_order = ".$row['id']);
									$total_row = mysql_fetch_array($total_result);
									$total_losses = $total_row['all_wins'];
									echo "<td>".($total_wins-$total_losses)." of ".$row['number_of_wins']." wins</td>";

									if($subrow['active'] == 1)
									{
										echo "<td>Active</td>";
										//echo "<td><a href='#' class='btn btn-success btn-xs'>Pause</a>";
										echo "<td><a href='duo_order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a> <a href='flag_duo.php?id=".$row['id']."' class='btn btn-success btn-xs'>Flag for admin</a></td>";
									}
									else if($subrow['active'] == 2)
									{
										echo "<td>Paused</td>";
										echo "<td><a href='#' class='btn btn-success btn-xs'>Resume</a>";
										echo "<a href='duo_order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
									}
									else if($subrow['active'] == 3)
									{
										echo "<td>Admin reviewing</td>";
										echo "<td><a href='duo_order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
									}
									else if($subrow['active'] == 0)
									{
										echo "<td>Complete</td>";
										echo "<td><a href='duo_order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a> <a href='flag_duo.php?id=".$row['id']."' class='btn btn-success btn-xs'>Flag for admin</a></td>";
									}
								}

								
								echo "</tr>";
								
							}
						}
					}
					else if($type == 1)
					{
						$result = mysql_query("SELECT * FROM booster_duo WHERE active <> 0 AND booster = ".$_SESSION['user']);

						if(mysql_num_rows($result) == 0)
						{
							echo "<tr><td colspan='4'>You don't have any active orders.</td></tr>";
						}
						else
						{
							while($row = mysql_fetch_array($result))
							{
								$order = $row['duo_order'];

								$subresult = mysql_query("SELECT * FROM duo_order WHERE id = $order");
								$subrow = mysql_fetch_array($subresult);

								echo "<tr><td>".$subrow['id']."</td>";

								$boost_result = mysql_query("SELECT * FROM user WHERE id = ".$row['booster']) or die(mysql_error());
								$boost_row = mysql_fetch_array($boost_result);
								$cur_booster = $boost_row['username'];

								echo "<td>".$cur_booster."</td>";
								echo "<td>".$subrow['summoner']."</td>";
								echo "<td>".strtoupper($subrow['region'])."</td>";

								if($subrow['number_of_games'] > 0)
									echo "<td>".$subrow['number_of_games']." games at ".$subrow['start_tier']." ".$subrow['start_division']."</td>";
								else if($subrow['number_of_wins'] > 0)
									echo "<td>".$subrow['number_of_wins']." wins at ".$subrow['start_tier']." ".$subrow['start_division']."</td>";
								else if($subrow['number_of_coaches'] > 0)
									echo "<td>".$subrow['number_of_coaches']." coached games at ".$subrow['start_tier']." ".$subrow['start_division']."</td>";

								$total_result = mysql_query("SELECT SUM(wins) AS all_wins FROM booster_duo WHERE duo_order = ".$subrow['id']);
								$total_row = mysql_fetch_array($total_result);
								$total_wins = $total_row['all_wins'];
								$total_result = mysql_query("SELECT SUM(losses) AS all_wins FROM booster_duo WHERE duo_order = ".$subrow['id']);
								$total_row = mysql_fetch_array($total_result);
								$total_losses = $total_row['all_wins'];
								echo "<td>".($total_wins-$total_losses)." of ".$subrow['number_of_wins']." wins</td>";

								if($row['active'] == 1)
								{
									echo "<td>Active</td>";
									echo "<td><a href='duo_order.php?id=".$subrow['id']."' class='btn btn-success btn-xs'>Details</a><a href='unclaim_duo.php?id=".$row['id']."' class='btn btn-success btn-xs'>Unclaim</a></td>";
								}
								else if($row['active'] == 2)
								{
									echo "<td>Paused</td>";
									echo "<td><a href='duo_order.php?id=".$subrow['id']."' class='btn btn-success btn-xs'>Details</a><a href='unclaim_duo.php?id=".$row['id']."' class='btn btn-success btn-xs'>Unclaim</a></td>";
								}
								else if($row['active'] == 3)
								{
									echo "<td>Admin reviewing</td>";
									echo "<td><a href='duo_order.php?id=".$subrow['id']."' class='btn btn-success btn-xs'>Details</a><a href='unclaim_duo.php?id=".$row['id']."' class='btn btn-success btn-xs'>Unclaim</a></td>";
								}
								echo "</tr>";
								
							}
						}
					}
					else if($type == 2 || $type == 3)
					{
						$result = mysql_query("SELECT * FROM duo_order WHERE complete = 0");

						if(mysql_num_rows($result) == 0)
						{
							echo "<tr><td colspan='4'>You don't have any orders.</td></tr>";
						}
						else
						{
							while($row = mysql_fetch_array($result))
							{
								
								echo "<tr><td>".$row['id']."</td>";

								$subresult = mysql_query("SELECT * FROM booster_duo WHERE active <> 0 AND duo_order = ".$row['id']) or die(mysql_error());

								if(mysql_num_rows($subresult) == 0)
								{
									$cur_booster = "None";
								}
								else
								{
									$subrow = mysql_fetch_array($subresult);
									$boost_result = mysql_query("SELECT * FROM user WHERE id = ".$subrow['booster']) or die(mysql_error());
									$boost_row = mysql_fetch_array($boost_result);
									$cur_booster = $boost_row['username'];
								}

								echo "<td>".$cur_booster."</td>";
								echo "<td>".$row['summoner']."</td>";
								echo "<td>".strtoupper($row['region'])."</td>";

								if($row['number_of_games'] > 0)
									echo "<td>".$row['number_of_games']." games at ".$row['start_tier']." ".$row['start_division']."</td>";
								else if($row['number_of_wins'] > 0)
									echo "<td>".$row['number_of_wins']." wins at ".$row['start_tier']." ".$row['start_division']."</td>";
								else if($row['number_of_coaches'] > 0)
									echo "<td>".$row['number_of_coaches']." coached games at ".$row['start_tier']." ".$row['start_division']."</td>";

								$subresult = mysql_query("SELECT * FROM booster_duo WHERE active <> 0 AND duo_order = ".$row['id']." ORDER BY id DESC");

								if(mysql_num_rows($subresult) == 0)
								{
									echo "<td>Order pending</td>";
									echo "<td>Pending</td>";
									echo "<td><a href='duo_order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
								}
								else
								{
									$subrow = mysql_fetch_array($subresult);
									$total_result = mysql_query("SELECT SUM(wins) AS all_wins FROM booster_duo WHERE duo_order = ".$row['id']);
									$total_row = mysql_fetch_array($total_result);
									$total_wins = $total_row['all_wins'];
									$total_result = mysql_query("SELECT SUM(losses) AS all_wins FROM booster_duo WHERE duo_order = ".$row['id']);
									$total_row = mysql_fetch_array($total_result);
									$total_losses = $total_row['all_wins'];
									echo "<td>".($total_wins-$total_losses)." of ".$row['number_of_wins']." wins</td>";

									if($subrow['active'] == 1)
									{
										echo "<td>Active</td>";
										//echo "<td><a href='#' class='btn btn-success btn-xs'>Pause</a>";
										echo "<td><a href='duo_order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
									}
									else if($subrow['active'] == 2)
									{
										echo "<td>Paused</td>";
										echo "<td><a href='#' class='btn btn-success btn-xs'>Resume</a>";
										echo "<a href='duo_order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
									}
									else if($subrow['active'] == 3)
									{
										echo "<td>Admin reviewing</td>";
										echo "<td><a href='duo_order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
									}
									else if($subrow['active'] == 0)
									{
										echo "<td>Complete</td>";
										echo "<td><a href='duo_order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
									}
								}

								
								echo "</tr>";
								
							}
						}
					}
				?>
			   </tbody>
            </table>
								</div>
							</div>
						</div>
					</div>
					<!-- Referral Orders -->
					<div class="row-fluid">
						<div class="col-xs-12">
							<div class="widget-box widget-calendar">
								<div class="widget-title">
								<span class="icon"><i class="fa fa-calendar"></i></span><h5>Referral Orders</h5>
								</div>
								<div class="widget-content nopadding">
								<table class="table table-bordered table-striped table-hover">
               <thead>
                  <tr>
				     <th>Order ID</th>
					 <th>Summoner</th>
                     <th>Details</th>
					 <th>Referral Link</th>
                     <th>Status</th>
                     <th>Actions</th>
                  </tr>
               </thead>
               <tbody>
			   <?php
					if($type != 2 && $type != 3)
					{
						$result = mysql_query("SELECT * FROM referral_order WHERE user = ".$_SESSION['user']." ORDER BY complete ASC");
					}
					else
					{
						$result = mysql_query("SELECT * FROM referral_order WHERE complete = 0");
					}

					if(mysql_num_rows($result) == 0)
					{
						echo "<tr><td colspan='3'>You don't have any orders.</td></tr>";
					}
					else
					{
						while($row = mysql_fetch_array($result))
						{
							echo "<tr>";
							echo "<td>".$row['id']."</td>";
							$user_result = mysql_query("SELECT * FROM user WHERE id = ".$row['user']);
							$user_row = mysql_fetch_array($user_result);
							echo "<td>".$user_row['summoner']."</td>";
							echo "<td>".$row['referrals']." referrals</td>";
							echo "<td>".$row['link']."</td>";

							if($row['complete'] == 0)
								echo "<td>In progress</td>";
							else
								echo "<td>Complete</td>";
							echo "<td><a href='referral_order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
						}
					}
				?>
			   </tbody>
            </table>
								</div>
							</div>
						</div>
					</div>
					<!-- -->
					<div class="row-fluid">
						<div class="col-xs-12">
							<div class="widget-box widget-calendar">
								<div class="widget-title">
								<span class="icon"><i class="fa fa-calendar"></i></span><h5>Leveling Orders</h5>
								</div>
								<div class="widget-content nopadding">
								<table class="table table-bordered table-striped table-hover">
								   <thead>
									  <tr>
										 <th>Order ID</th>
										 <th>Summoner</th>
										 <th>Starting Level</th>
										 <th>Desired Level</th>
										 <th>Status</th>
										 <th>Actions</th>
									  </tr>
								   </thead>
								   <tbody>
								   <?php
										if($type != 2 && $type != 3)
										{
											$result = mysql_query("SELECT * FROM leveling_order WHERE user = ".$_SESSION['user']." ORDER BY complete ASC");
										}
										else
										{
											$result = mysql_query("SELECT * FROM leveling_order WHERE complete = 0");
										}

										if(mysql_num_rows($result) == 0)
										{
											echo "<tr><td colspan='3'>You don't have any orders.</td></tr>";
										}
										else
										{
											while($row = mysql_fetch_array($result))
											{
												echo "<tr>";
												echo "<td>".$row['id']."</td>";
												echo "<td>".$row['summoner']."</td>";
												echo "<td>".$row['start_level']."</td>";
												/*if($row['complete'] != 0)
												{
													echo "<td>".($row['start_level'] + $row['levels'])."</td>";
												}
												else
												{
													$newresponse = Unirest::get(
													  "https://teemojson.p.mashape.com/player/".$row['region']."/".str_replace(" ", "", $row['summoner']),
													  array(
														"X-Mashape-Authorization" => "XcM4wvOlSewxmSNGstMYMpkjxvZGc8gx"
													  ),
													  null
													);
													$decode = json_decode($newresponse->raw_body)->data->level;
													echo "<td>".$decode."</td>";
												}*/
												echo "<td>".($row['start_level'] + $row['levels'])."</td>";
												if($row['complete'] == 0)
													echo "<td>In progress</td>";
												else
													echo "<td>Complete</td>";
												echo "<td><a href='leveling_order.php?id=".$row['id']."' class='btn btn-success btn-xs'>Details</a></td>";
											}
										}
									?>
								   </tbody>
								</table>
								</div>
							</div>
						</div>
					</div>
					<!-- -->
					
					<div class="row-fluid">
						<div class="col-xs-12">
							<div class="widget-box widget-calendar">
								<div class="widget-title">
								<span class="icon"><i class="fa fa-calendar"></i></span><h5>Account Orders</h5>
								</div>
								<div class="widget-content nopadding">
								<table class="table table-normal">
								   <thead>
									  <tr>
										 <th>Order ID</th>
										 <th>Purchase Date</th>
										 <th>Login</th>
										 <th>Password</th>
										 <th>Region</th>
									  </tr>
								   </thead>
								   <tbody id = "complete_account_records">
								   <?php
										$result = mysql_query("SELECT * FROM account_order WHERE user = $user") or die(mysql_error());

										while($row = mysql_fetch_array($result))
										{
											$user_result = mysql_query("SELECT * FROM user WHERE id = ".$row['user']);
											$user_row = mysql_fetch_array($user_result);

											$account_result = mysql_query("SELECT * FROM bulk_account WHERE order_id = ".$row['id']);
											$account_row = mysql_fetch_array($account_result);

											echo "<tr><td>".$row['id']."</td>";
											echo "<td>".date("d M Y", $row['timestamp'])."</td>";
											echo "<td>".$account_row['username']."</td>";
											echo "<td>".$account_row['password']."</td>";
											echo "<td>".$account_row['region']."</td>";
											echo "</tr>";
										}
										
									?>
								   </tbody>
								</table>
								</div>
							</div>
						</div>
					</div>
				<?php
	if(($type == 1 && $level != 0) || $type == 2 || $type == 3)
	{
		echo '
<div class = "box">
<div class="box-header">
      <div class="title">Boosters Chat</div>
   </div>
   <div class="box-content">
         <ul class="chat-box">
            <div id="chat-messages-inner">';

			$result = mysql_query("SELECT * FROM booster_chat ORDER BY timestamp DESC");

			$messages = array();

			while($row = mysql_fetch_array($result))
			{
				$subresult = mysql_query("SELECT * FROM user WHERE id = ".$row['user']);
				$subrow = mysql_fetch_array($subresult);

				$row['name'] = $subrow['username'];
				$row['email'] = $subrow['paypal'];
				$row['type'] = $subrow['type'];

				array_push($messages, $row);
			}
			
			$x = 0;

			while(isset($messages[$x]))
			{
				echo '<li class="arrow-box-left gray" id="msg-'.$messages[$x]['id'].'">';
				echo '<div class="avatar"><img class="avatar-small" src="images/avatars/'.$messages[$x]['type'].'.png"></div>';
				echo '<div class="info"><span class="name"><strong>'.$messages[$x]['name'].'</strong>';

				if($messages[$x]['type'] == 2)
					echo '<span class="badge badge-red">administrator</span>';
				else if($messages[$x]['type'] == 3)
					echo '<span class="badge badge-red">manager</span>';
				else
					echo '<span class="badge badge-blue">booster</span>';
				
				if($type == 2)
					echo "<span class='badge badge-red' style = 'margin-left: 5px; cursor: pointer;' onclick = 'delete_booster_chat(".$messages[$x]['id'].")'>Remove</span>";

				echo '</span><span class="time">'.date("d M Y g:i:sA", $messages[$x]['timestamp']).'</span></div>';
				echo stripslashes($messages[$x]['message']);
				echo '</li><li class="divider"></li>';
				$x++;
			}
			echo '

</div>
         </ul>';
      
		 echo '
                     
   </div>
   <div class="box-footer flat padded" style = "height: 56px">

            <div class="input-append">
               <input type="text" name="msg-box" id="msg-box" onkeyup = "check_chat_send(event)">
               <ul class="add-on" >
                  <li>
                     <button class="btn btn-success btn-xs" type="submit" onclick = "chat_send()">Send</button>
                  </li>
               </ul>
            </div>

   </div>
</div>';
	}
	?>
</div>
<script>
function check_chat_send (event)
{
	if(event.keyCode == 13)
	{
		if(document.getElementById("msg-box").value != "")
		{
			chat_send();
		}
	}
}
function chat_send ()
{
	if(document.getElementById("msg-box").value != "")
	{
		var ajaxHandler = new XMLHttpRequest();

		ajaxHandler.onreadystatechange = function ()
		{
			if(ajaxHandler.readyState == 4)
			{
				update_booster_chat();
				document.getElementById("msg-box").value = "";
			}
		}

		var form = new FormData();
		form.append("msg-box", document.getElementById("msg-box").value);

		ajaxHandler.open("POST", "chat-send.php");
		ajaxHandler.send(form);
	}
}
function delete_booster_chat (id)
{
	var ajaxHandler = new XMLHttpRequest();

	var form = new FormData();
	form.append("id", id);

	ajaxHandler.open("POST", "delete_booster_chat.php");
	ajaxHandler.send(form);

	var target = document.getElementById("msg-"+id);
	target.parentNode.removeChild(target);
}
function update_booster_chat ()
{
	var ajaxHandler = new XMLHttpRequest();

	ajaxHandler.onreadystatechange = function ()
	{
		if(ajaxHandler.readyState == 4)
		{
			var chat_box = document.getElementById("chat-messages-inner");

			var messages = eval( '(' + ajaxHandler.responseText + ')' );

			var i = 0;

			chat_box.innerHTML = "";
			var html_buffer = "";

			while(messages[i] != null)
			{
				html_buffer += '<li class="arrow-box-left gray" id="msg-'+messages[i]['id']+'">';
				html_buffer += '<div class="avatar"><img class="avatar-small" src="images/avatars/'+messages[i]['type']+'.png"></div>';
				html_buffer += '<div class="info"><span class="name"><strong>'+messages[i]['name']+'</strong>';

				if(messages[i]['type'] == 1)
					html_buffer += '<span class="badge badge-blue">booster</span>';
				else
					html_buffer += '<span class="badge badge-red">administrator</span>';

				<?php
					if($type == 2)
						echo "html_buffer += '<span class=\"badge badge-red\" style = \"margin-left: 5px; cursor: pointer;\" onclick = \"delete_booster_chat('+messages[i]['id']+')\">Remove</span>';";
				?>

				html_buffer += '</span><span class="time">'+messages[i]['time']+'</span></div>';
				html_buffer += messages[i]['message'];
				html_buffer += '</li><li class="divider"></li>';
				i++;
			}

			chat_box.innerHTML = html_buffer;
		}
	}

	ajaxHandler.open("POST", "booster-chat-refresh.php");
	ajaxHandler.send();
}
function updateTimer ()
{
	update_booster_chat();
	setTimeout(updateTimer, 10000);
}
window.onload = function ()
{
	<?php
		if($type != 0)
			echo 'updateTimer();';
	?>
}
</script>
</div>
<?php
	include("bottom.php");
?>
